package com.fintech.third.utils.wechat;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpStatus;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import com.marvels.common.utils.MarvelsLogUtil;


public class HttpUtils {
	
	
	public final static String USER_AGENT_H = "User-Agent";

	public final static String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0";

	public final static String UTF8 = "UTF-8";
	
	
	
	
	/**
	 * get请求
	 * 
	 * @param getUrl
	 * @return
	 */
	public static String getContentData(String getUrl, String charset) {
		try {
			HttpClient httpClient = new HttpClient();
			GetMethod get = new GetMethod(getUrl);
			get.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,
					charset);
			get.setRequestHeader(USER_AGENT_H, USER_AGENT);
			int status = httpClient.executeMethod(get);
			if (status == HttpStatus.SC_OK) {
				String ret = get.getResponseBodyAsString();
				return ret;
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(e.getMessage());
			System.out.println(e);
		}
		return "";
	}
	
	
	
	/**
	 * post请求
	 * 
	 * @param urlPath
	 * @param requestData
	 * @param charset
	 * @return
	 */
	public static String postContentData(String urlPath, String requestData,
			String charset) {
		try {
			HttpClient httpClient = new HttpClient();
			PostMethod post = new PostMethod(urlPath);
			post.setRequestHeader(USER_AGENT_H, USER_AGENT);
			post.getParams().setParameter(
					HttpMethodParams.HTTP_CONTENT_CHARSET, charset);
			post.setRequestBody(requestData);
			int status = httpClient.executeMethod(post);
			if (status == HttpStatus.SC_OK) {
				String ret = post.getResponseBodyAsString();
				return ret;
			}
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("", e);
		}
		return "";
	}
	
	
	/**
	 * 
	* @Title: SMS 
	* @Description: 互盟推送短信调用 
	* @param postData
	* @param postUrl
	* @return       
	* @author 李瑶
	* @date 2018年3月26日 下午4:39:32
	 */
	public static String SMS(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);
            conn.setConnectTimeout(60000);
            conn.setReadTimeout(60000);
            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
            MarvelsLogUtil.getInstance().error(">>>>>>>>>>互盟短信发送异常:"+e);
        }
        return "";
    }

	
	public static void main(String[] args) {
	}
}
